<template>
  <view>
    <view class="search-box">
      <uni-search-bar @input="input()" :value="kw" :radius="100" cancelButton="none" bgColor="#c00000"></uni-search-bar>
    </view>
    <view class="sugg-list" v-if="searchResults.length !== 0">
      <view class="sugg-item" v-for = "(item,i) in searchResults" :key="i" @click="gotoDerail(item.goods_id)">
        <view class="goods-name">
          {{item.goods_name}}
        </view>
        <uni-icons type="arrowright" size="16"></uni-icons>
      </view>
    </view>
      
      <view class="history-box" v-else>
        
        <view class="history-title" >
          <text>搜索历史</text>
          <uni-icons type="trash" size="17" @click="rmhistoryList()"></uni-icons>
        </view>
        
        <view class="history-list">
            <uni-tag :text="item" v-for="(item,i) in historyList" :key="i" :customStyle="'display: inline-block;margin: 5px 5px 0 0;'" @click="addkw(item)"></uni-tag>
        </view>
        
      </view>
  </view>
</template>

<script>
  export default {
    data() {
      return {
        time : null,
        kw:'',
        searchResults:[],
        // 历史记录
        historyList:[]
      };
    },
    onLoad() {
      this.historyList = JSON.parse(uni.getStorageSync('kw') )|| '[]'
    },
    methods:{
      input(e){
        clearTimeout(this.timer)
        this.timer = setTimeout(()=>{
          this.kw = e.trim()
          this.getSearchList()
          this.savaHistory()
        },500)
      },
      async getSearchList(){
        if(this.kw === ''){
          this.searchResults = []
          return 
        }
        const {data : res} = await uni.$http.get('/api/public/v1/goods/qsearch',{
          query : this.kw
        })
        if (res.meta.status !== 200) return uni.$showMsg()
        this.searchResults = res.message
      },
      // 跳转搜索
      gotoDerail(goods_id){
        uni.navigateTo({
          url:"/subpkg/goods_detail/goods_detail?goods_id=" + goods_id
        })
        
      },
      // 加入搜索历史
      savaHistory(){
        if (this.historyList.indexOf(this.kw) != -1)
        {
          this.historyList.splice(this.historyList.indexOf(this.kw),1);
          this.historyList.unshift(this.kw)
        }
        else{
          this.historyList.unshift(this.kw)
          // this.historyList = Array.from(new Set(this.historyList))
        }
        if (this.historyList.length>=10){
          this.historyList = this.historyList.slice(0,10)
        }
        uni.setStorageSync('kw',JSON.stringify(this.historyList))
      },
      //清空搜索历史
      rmhistoryList(){
        
        uni.showModal({
        				title: '提示',
        				content: '确定删除搜索记录',
        				showCancel:true,
        				success: function (res) {
        					if (res.confirm) {
                    this.historyList = []
                    uni.setStorageSync('kw','[]')
                    uni.showToast({
                    				title: '删除成功',
                    				icon:'exception',
                    				duration:850
                    			});
        					} else if (res.cancel) {
        						console.log('取消');
        					}
        				}.bind(this)
        			});
      },
      //点击搜索历史
      addkw(item){
        this.kw = item
        this.getSearchList()
        this.savaHistory()
      }
    }
  }
</script>

<style lang="scss">
.search-box{
  background-color: #fff;
  position: sticky;
  top: 0px;
  z-index: 999;
}
.sugg-list{
  padding: 0 10px;
  .sugg-item{
    font-size: 12px;
    padding: 13px 0;
    display: flex;
    border-bottom: 1px solid #efefef;
    align-items: center;
    justify-content: space-between;
  }
  .goods-name{
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    margin-right: 3px;
  }
  }
  .history-box{
    padding: 0 10px;
    .history-title{
      display: flex;
      width: 100%;
      justify-content: space-between;
      align-items: center;
      height: 40px;
      font-size: 13px;
      border-bottom: 1px solid #efefef;
    }
    .history-list{
      display: flex;
      flex-wrap: wrap;
       
    }
  
}
</style>
